/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.logic.db;

import java.util.Iterator;
import java.util.Vector;
import model.logic.nondb.CommonMethods;
import model.pojo.AboutUs;
import model.pojo.ContactUsMsg;
import model.pojo.News;
import model.pojo.NewsCategories;
import model.pojo.Products;
import model.pojo.Services;
import model.pojo.Settings;
import model.pojo.Vacancies;
import model.utils.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 * this class contain functionalities that is common between user,applicant and
 * unregistered member
 *
 * @author Maha
 */
public class CommonWSFunctionality {

    /**
     *
     * This is Hibernate Session
     */
    Session session;

    /**
     *
     * Contain Code to get SessionFactory, from ${@link HibernateUtil} class
     */
    public CommonWSFunctionality() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * get aboutus table data
     */
    public Vector<AboutUs> getAboutUsMenuAndContents() {
        Vector<AboutUs> aboutus = new Vector<AboutUs>();
        Criteria criteria = session.createCriteria(AboutUs.class);
        Iterator iterator = criteria.list().iterator();
        while (iterator.hasNext()) {
            aboutus.add((AboutUs) iterator.next());
        }
        return aboutus;
    }

    /**
     * get single aboutus data
     */
    public AboutUs getSelectedAboutUs(String Header) {
        Criteria crit = session.createCriteria(AboutUs.class).add(Restrictions.ilike("aboutUsTerm", Header));
        AboutUs object = null;
        Iterator iterator = crit.list().iterator();
        while (iterator.hasNext()) {
            object = (AboutUs) iterator.next();
        }
        return object;
    }

    /**
     * get single news from news table by name
     */
    public News getSelectedNews(String news) {
        Criteria crit = session.createCriteria(News.class).add(Restrictions.ilike("newsTitle", news));
        News object = null;
        Iterator iterator = crit.list().iterator();
        while (iterator.hasNext()) {
            object = (News) iterator.next();
        }
        return object;
    }

    /**
     * get products table data
     */
    public Vector<Products> getProductsAndContents() {
        Vector<Products> products = new Vector<Products>();
        Iterator iterator = session.createCriteria(Products.class).list().iterator();
        while (iterator.hasNext()) {
            products.add((Products) iterator.next());
        }
        return products;

    }

    public Services getSelectedService(String serviceName) {
        Criteria crit = session.createCriteria(Services.class).add(Restrictions.ilike("serviceName", serviceName));
        Services object = null;
        Iterator iterator = crit.list().iterator();
        while (iterator.hasNext()) {
            object = (Services) iterator.next();
        }
        return object;
    }

    public Products getSelectedProductData(String ProductName) {
        Criteria crit = session.createCriteria(Products.class).add(Restrictions.ilike("productName", ProductName));
        Products object = null;
        Iterator iterator = crit.list().iterator();
        while (iterator.hasNext()) {
            object = (Products) iterator.next();
        }
        return object;
    }

    /**
     * get service table data
     */
    public Vector<Services> getServicesAndContents() {
        Vector<Services> serv = new Vector<Services>();
        Iterator iterator = session.createCriteria(Services.class).list().iterator();
        while (iterator.hasNext()) {
            serv.add((Services) iterator.next());
        }
        return serv;
    }

    /**
     * add comment that is sent by website user to database
     */
    public int insertComment(ContactUsMsg comment) {
        int x;
        session.beginTransaction();
        try {
            session.save(comment);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    /**
     * get news table data
     */
    public Vector<News> getNews() {
        Vector<News> news = new Vector<News>();
        Iterator iterator = session.createCriteria(News.class).list().iterator();
        while (iterator.hasNext()) {
            news.add((News) iterator.next());

        }
        return news;
    }

    /**
     * get news categories table data
     */
    public Vector<NewsCategories> getCat() {
        Vector<NewsCategories> cat = new Vector<NewsCategories>();
        Iterator iterator = session.createCriteria(NewsCategories.class).list().iterator();
        while (iterator.hasNext()) {
            cat.add((NewsCategories) iterator.next());

        }
        return cat;
    }

    public Vector<News> getNewsOREvent(int id) {
        Vector<News> cat = new Vector<News>();
        Iterator iterator = session.createCriteria(News.class).add(Restrictions.eq("newsCategories.newsCategoryId", id)).list().iterator();
        while (iterator.hasNext()) {
            cat.add((News) iterator.next());
        }
        return cat;
    }

    public News getOneNewsOREvent(int id) {
        News cat = new News();
        Iterator iterator = session.createCriteria(News.class).add(Restrictions.eq("newsId", id)).list().iterator();
        while (iterator.hasNext()) {
            cat = (News) iterator.next();
        }
        return cat;
    }

    /**
     * get selected news by id and returns Vector of news
     */
    public Vector<News> getnews(int id) {
        Vector<News> cat = new Vector<News>();
        Iterator iterator = session.createCriteria(News.class).add(Restrictions.eq("newsId", id)).list().iterator();
        while (iterator.hasNext()) {
            cat.add((News) iterator.next());
        }
        return cat;
    }

    /**
     * get available vacancies to be displayed
     */
    public Vector<Vacancies> getVacancies() {
        Vector<Vacancies> vacancies = new Vector<Vacancies>();
        Iterator iterator = session.createCriteria(Vacancies.class).list().iterator();
        while (iterator.hasNext()) {
            vacancies.add((Vacancies) iterator.next());
        }
        return vacancies;
    }

    /**
     * get settings from settings table
     */
    public Settings getSettings() {
        Settings settings = null;
        Iterator iterator = session.createCriteria(Settings.class).list().iterator();
        while (iterator.hasNext()) {
            settings = (Settings) iterator.next();
        }
        return settings;
    }
}
